if (typeof inizializza_datepickers != 'undefined') {
/* Facciamo l'inizializzazione solo sulle pagine che effettivamente contengono una select */

$(document).ready(function(){
	
	// The select element to be replaced:
	var select = $('select.makeMeFancy');
	select.show();

	var selectBoxContainer = $('<div>',{
		width		: select.outerWidth(),
		className	: 'tzSelect',
		html		: '<div class="selectBox"></div>'
	});

	var dropDown = $('<ul>',{className:'dropDown'});
	var selectBox = selectBoxContainer.find('.selectBox');
	
	// Looping though the options of the original select element
	
	select.find('option').each(function(i){
		var option = $(this);
		
		if(i==select.attr('selectedIndex')){
			selectBox.html(option.text());
		}
		
		// As of jQuery 1.4.3 we can access HTML5 
		// data attributes with the data() method.
		
		if(option.data('skip')){
			return true;
		}
		
		// Creating a dropdown item according to the
		// data-icon and data-html-text HTML5 attributes:
		var li;
		var event_data = new Date();
		event_data.setTime(event_data.getTime() );
		if (option.data('periodo')) {
			li = $('<li>', {
				html : '<hr /><label for="datepick_inizio">Da: </label><input type="text" id="datepick_inizio" /><br /><label for="datepick_fine">A: </label><input type="text" id="datepick_fine" /><p class="nota_formato_data">(giorno-mese-anno)</p>'
			});
			li.addClass('picker_periodo');
			li.css("height","88px");
		} else if (option.data('noimg') == 'no'){
			li = $('<li>',{
				html: aux = '<img src="'+option.data('icon')+'" />'//+option.data('time')+" "+event_data.getDate()+"-"+(event_data.getMonth()+1)+"-"+event_data.getFullYear()
			});
			li.css("height","40px");
			
		} else {
			li = $('<li>',{
				html: '<img src="'+option.data('icon')+'" /><span>'+ option.data('html-text')+'</span>'
			});
		}
		
		li.click(function(){
			
			if (!option.data('periodo')) {
				selectBox.html(option.text());
				dropDown.trigger('hide');
				// When a click occurs, we are also reflecting
				// the change on the original select element:
				select.val(option.val());

				// Navighiamo all'URL desiderata
				window.location.href = option.data('destinazione');
			}

			return false;
		});
		
		dropDown.append(li);
	});
	
	selectBoxContainer.append(dropDown.hide());
	select.hide().after(selectBoxContainer);
	
	// Binding custom show and hide events on the dropDown:
	
	dropDown.bind('show',function(){
		
		if(dropDown.is(':animated')){
			return false;
		}
		
		selectBox.addClass('expanded');
		dropDown.slideDown();
		
	}).bind('hide',function(){
		
		if(dropDown.is(':animated')){
			return false;
		}
		
		selectBox.removeClass('expanded');
		dropDown.slideUp();
		
	}).bind('toggle',function(){
		if(selectBox.hasClass('expanded')){
			dropDown.trigger('hide');
		}
		else dropDown.trigger('show');
	});
	
	selectBox.click(function(){
		dropDown.trigger('toggle');
		return false;
	});

	// If we click anywhere on the page, while the
	// dropdown is shown, it is going to be hidden:
	
	$(document).click(function(e){
		if (!$(e.target).hasClass('ui-state-default'))
			dropDown.trigger('hide');
	});

	inizializza_datepickers();
});

} /* typeof inizializza_datepickers */
